package com.budwk.app.util;

import cn.hutool.core.bean.BeanUtil;
import com.alibaba.fastjson.JSONObject;
import com.budwk.app.common.config.AppException;
import com.budwk.app.sqfw.constants.CommonConstants;
import com.budwk.app.sqfw.dto.SqfwServicerecordForAdminDTO;
import com.budwk.app.sqfw.models.Sqfw_cenSusPaper;
import com.budwk.app.sqfw.vo.AreaAddressVo;
import com.budwk.app.sqfw.vo.Sqfw_formSummaryForAdminVO;
import com.budwk.app.sys.models.Sys_user;
import com.budwk.app.sys.services.SysUserService;
import com.budwk.app.util.poiUtils.nutzBeanUtils.NutzIocUtils;
import com.budwk.starter.security.utils.SecurityUtil;
import org.apache.commons.lang3.StringUtils;
import org.nutz.dao.util.cri.SqlExpressionGroup;

/**
 * 权限管理工具类
 */

public class AuthorityUtil {

    private static Sys_user checkUser() {
        SysUserService sysUserService = (SysUserService) NutzIocUtils.getBeanByType(SysUserService.class);
        Sys_user user = sysUserService.fetch(SecurityUtil.getUserId());
        if (user == null) {
            throw new AppException("用户未登录！");
        }
        return user;
    }

    /**
     * 校验用户输入的地址信息
     * @param forAdminVo
     */
    public static AreaAddressVo checkFormSummaryAreaPermission(Sqfw_formSummaryForAdminVO forAdminVo) {
        Sys_user user = checkUser();
        AreaAddressVo areaAddressVo = new AreaAddressVo();
        areaAddressVo.setProvince(forAdminVo.getProvince())
                .setCity(forAdminVo.getCity())
                .setDistrict(forAdminVo.getDistrict())
                .setCounty(forAdminVo.getCounty())
                .setCommunity(forAdminVo.getCommunity());
        buildAreaAddressVo(user, areaAddressVo);
        // 赋值
        forAdminVo.setProvince(StringUtils.isNotEmpty(areaAddressVo.getProvince()) ? areaAddressVo.getProvince() : null);
        forAdminVo.setCity(StringUtils.isNotEmpty(areaAddressVo.getCity()) ? areaAddressVo.getCity() : null);
        forAdminVo.setDistrict(StringUtils.isNotEmpty(areaAddressVo.getDistrict()) ? areaAddressVo.getDistrict() : null);
        forAdminVo.setCounty(StringUtils.isNotEmpty(areaAddressVo.getCounty()) ? areaAddressVo.getCounty() : null);
        forAdminVo.setCommunity(StringUtils.isNotEmpty(areaAddressVo.getCommunity()) ? areaAddressVo.getCommunity() : null);

        return areaAddressVo;
    }

    public static AreaAddressVo checkCenSusPaperAreaPermission(Sqfw_cenSusPaper bean) {
        Sys_user user = checkUser();
        AreaAddressVo areaAddressVo = new AreaAddressVo();
        areaAddressVo.setProvince(bean.getProvince())
                .setCity(bean.getCity())
                .setDistrict(bean.getDistrict())
                .setCounty(bean.getCounty())
                .setCommunity(bean.getCommunity());
        buildAreaAddressVo(user, areaAddressVo);
        // 赋值
        bean.setProvince(StringUtils.isNotEmpty(areaAddressVo.getProvince()) ? areaAddressVo.getProvince() : null);
        bean.setCity(StringUtils.isNotEmpty(areaAddressVo.getCity()) ? areaAddressVo.getCity() : null);
        bean.setDistrict(StringUtils.isNotEmpty(areaAddressVo.getDistrict()) ? areaAddressVo.getDistrict() : null);
        bean.setCounty(StringUtils.isNotEmpty(areaAddressVo.getCounty()) ? areaAddressVo.getCounty() : null);
        bean.setCommunity(StringUtils.isNotEmpty(areaAddressVo.getCommunity()) ? areaAddressVo.getCommunity() : null);
        return areaAddressVo;
    }

    // 判断用户是否属于直属
    public static boolean isZhiShu(Sys_user user, AreaAddressVo bean) {
        String[] addressStr = {user.getProvince(), user.getCity(), user.getDistrict(), user.getCounty(), user.getCommunity()};
        String[] addressNames = {"province", "city", "district", "county", "community"};
        // 将原数据转为json
        JSONObject jsonObject = (JSONObject) JSONObject.toJSON(bean);
        boolean isZhiShu = false;
        for (int i = 0; i < addressStr.length; i++) {
            String address = addressStr[i];
            // 如果是直属
            if (StringUtils.isNotEmpty(address)) {
                if (address.equals(CommonConstants.DIRECTLY_UNDER)) {
                    isZhiShu = true;
                    break;
                }
                jsonObject.put(addressNames[i], address);
            }
        }
        AreaAddressVo areaAddressVo = JSONObject.toJavaObject(jsonObject, AreaAddressVo.class);
        if (areaAddressVo != null) {
            BeanUtil.copyProperties(areaAddressVo, bean);
        }
        return isZhiShu;
    }

    public static AreaAddressVo checkInputInfo(Sys_user user, AreaAddressVo bean) {
        if (StringUtils.isNotEmpty(bean.getProvince())) {
            return bean;
        }
        if (StringUtils.isNotEmpty(bean.getCity())) {
            bean.setProvince(user.getProvince());
            return bean;
        }
        if (StringUtils.isNotEmpty(bean.getDistrict())) {
            bean.setProvince(user.getProvince()).setCity(user.getCity());
            return bean;
        }
        if (StringUtils.isNotEmpty(bean.getCounty())) {
            bean.setProvince(user.getProvince()).setCity(user.getCity()).setDistrict(user.getDistrict());
            return bean;
        }
        if (StringUtils.isNotEmpty(bean.getCommunity())) {
            bean.setProvince(user.getProvince()).setCity(user.getCity()).setDistrict(user.getDistrict()).setCounty(user.getCounty());
            return bean;
        }
        return bean;
    }


    private static AreaAddressVo buildAreaAddressVo(Sys_user user, AreaAddressVo bean) {
        if (isZhiShu(user, bean)) {
            return bean;
        }
        // 判断用户输入的信息
        return checkInputInfo(user, bean);
    }

    public static void checkServicerecordAreaPermission(SqfwServicerecordForAdminDTO bean) {
        // 检查用户
        Sys_user user = checkUser();
        // 构建信息
        AreaAddressVo areaAddressVo = new AreaAddressVo();
        areaAddressVo.setProvince(bean.getProvince())
                .setCity(bean.getCity())
                .setDistrict(bean.getDistrict())
                .setCounty(bean.getCounty())
                .setCommunity(bean.getCommunity());
        buildAreaAddressVo(user, areaAddressVo);
        // 赋值
        bean.setProvince(StringUtils.isNotEmpty(areaAddressVo.getProvince()) ? areaAddressVo.getProvince() : null);
        bean.setCity(StringUtils.isNotEmpty(areaAddressVo.getCity()) ? areaAddressVo.getCity() : null);
        bean.setDistrict(StringUtils.isNotEmpty(areaAddressVo.getDistrict()) ? areaAddressVo.getDistrict() : null);
        bean.setCounty(StringUtils.isNotEmpty(areaAddressVo.getCounty()) ? areaAddressVo.getCounty() : null);
        bean.setCommunity(StringUtils.isNotEmpty(areaAddressVo.getCommunity()) ? areaAddressVo.getCommunity() : null);
    }

    /**
     * 公共查询 五级权限（模糊查询）
     */
    public static void commonPermissionLikeQuery(SqlExpressionGroup expressionSql, AreaAddressVo areaAddressVo) {
        // 省
        if (StringUtils.isNotEmpty(areaAddressVo.getProvince())) {
            expressionSql.andLike(AreaAddressVo::getProvince, areaAddressVo.getProvince());
        }
        // 市
        if (StringUtils.isNotEmpty(areaAddressVo.getCity())) {
            expressionSql.andLike(AreaAddressVo::getCity, areaAddressVo.getCity());
        }
        // 区县
        if (StringUtils.isNotEmpty(areaAddressVo.getDistrict())) {
            expressionSql.andLike(AreaAddressVo::getDistrict, areaAddressVo.getDistrict());
        }
        // 街道
        if (StringUtils.isNotEmpty(areaAddressVo.getCounty())) {
            expressionSql.andLike(AreaAddressVo::getCounty, areaAddressVo.getCounty());
        }
        // 社区
        if (StringUtils.isNotEmpty(areaAddressVo.getCommunity())) {
            expressionSql.andLike(AreaAddressVo::getCommunity, areaAddressVo.getCommunity());
        }
    }

    /**
     * 公共查询 五级权限（==查询）
     */
    public static void commonPermissionEqualQuery(SqlExpressionGroup expressionSql, AreaAddressVo areaAddressVo) {
        // 省
        if (StringUtils.isNotEmpty(areaAddressVo.getProvince())) {
            expressionSql.andEquals(AreaAddressVo::getProvince, areaAddressVo.getProvince());
        }
        // 市
        if (StringUtils.isNotEmpty(areaAddressVo.getCity())) {
            expressionSql.andEquals(AreaAddressVo::getCity, areaAddressVo.getCity());
        }
        // 区县
        if (StringUtils.isNotEmpty(areaAddressVo.getDistrict())) {
            expressionSql.andEquals(AreaAddressVo::getDistrict, areaAddressVo.getDistrict());
        }
        // 街道
        if (StringUtils.isNotEmpty(areaAddressVo.getCounty())) {
            expressionSql.andEquals(AreaAddressVo::getCounty, areaAddressVo.getCounty());
        }
        // 社区
        if (StringUtils.isNotEmpty(areaAddressVo.getCommunity())) {
            expressionSql.andEquals(AreaAddressVo::getCommunity, areaAddressVo.getCommunity());
        }
    }
}
